package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.api.rest.MemberFootprintApi;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MemberFootprint;
import com.jic.member.request.MemberFootprintRequest;
import com.jic.member.response.MemberFootprintResponse;
import com.jic.member.service.MemberFootprintService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 会员足迹管理系统的服务
 */
@RestController
@RequestMapping("/memberFootprint")
public class MemberFootprintController  implements MemberFootprintApi {

    @Autowired
    MemberFootprintService memberFootprintService;


    /*
    * 分页查询
    * */
    @ApiOperation(value = "获取所有会员足迹")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<MemberFootprintResponse>> queryList(@RequestBody MemberFootprintRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MemberFootprintRequest memberFootprintRequest = BeanUtil.copy(form, MemberFootprintRequest.class);
        PageResult<MemberFootprint> result = memberFootprintService.queryPage(memberFootprintRequest, page);
        List<MemberFootprintResponse> memberFootprintResponseList = result.getRows().stream().map(temp -> {
            MemberFootprintResponse memberFootprintResponse = BeanUtil.copy(temp, MemberFootprintResponse.class);
            return memberFootprintResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MemberFootprintResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberFootprintResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "获取所有会员足迹")
    @ResponseBody
    @RequestMapping("/queryAll")
    public RestResult<List<MemberFootprintResponse>> queryAll(@RequestBody MemberFootprintRequest form) {
        MemberFootprintRequest memberFootprintRequest = BeanUtil.copy(form, MemberFootprintRequest.class);
        List<MemberFootprint> list = memberFootprintService.queryAll(memberFootprintRequest);
        List<MemberFootprintResponse> memberFootprintResponseList = list.stream().map(temp -> {
            MemberFootprintResponse memberFootprintResponse = BeanUtil.copy(temp, MemberFootprintResponse.class);
            return memberFootprintResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(memberFootprintResponseList);
    }

    /*
     * 新增会员足迹
     * */
    @ApiOperation(value = "新增会员足迹")
    @ResponseBody
    @RequestMapping("/addMemberFootprint")
    @Override
    public RestResult addMemberFootprint(@RequestBody MemberFootprintRequest form) {
        MemberFootprint memberFootprintRequest = BeanUtil.copy(form, MemberFootprint.class);
        long insert = memberFootprintService.insert(memberFootprintRequest);
        return RestResult.success(insert);
    }
}
